package com.lhl.kafka.kafkaconsumer;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.config.KafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.listener.AbstractMessageListenerContainer;
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;
import org.springframework.kafka.listener.ConsumerAwareListenerErrorHandler;
import org.springframework.kafka.listener.ContainerProperties;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.MessageHeaders;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
@EnableKafka
public class KafkaConfig {
    private String group;
    @Bean
    KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>>
                        kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory =
                                new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        factory.setConcurrency(1);

        factory.getContainerProperties().setPollTimeout(3000);
        factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL);
        factory.setBatchListener(true);
        return factory;
    }

    @Bean
    public ConsumerFactory<String, String> consumerFactory() {
        return new DefaultKafkaConsumerFactory<>(consumerConfigs(), new StringDeserializer(),new StringDeserializer());
    }

    @Bean
    public Map<String, Object> consumerConfigs() {
        System.out.println("-------------------"+group);
        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "129.1.99.3:9092");
        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
        props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 5);
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");//latest、earliest
        return props;
    }

//    @Bean(name = "consumerAwareListenerErrorHandler")
//    public ConsumerAwareListenerErrorHandler listen10ErrorHandler() {
//        return (m, e, c) -> {
//            MessageHeaders headers = m.getHeaders();
//            List<String> topics = headers.get(KafkaHeaders.RECEIVED_TOPIC, List.class);
//            List<Integer> partitions = headers.get(KafkaHeaders.RECEIVED_PARTITION_ID, List.class);
//            List<Long> offsets = headers.get(KafkaHeaders.OFFSET, List.class);
//            Map<TopicPartition, Long> offsetsToReset = new HashMap<>();
//            for (int i = 0; i < topics.size(); i++) {
//                int index = i;
//                offsetsToReset.compute(new TopicPartition(topics.get(i), partitions.get(i)),
//                        (k, v) -> v == null ? offsets.get(index) : Math.min(v, offsets.get(index)));
//            }
//            offsetsToReset.forEach((k, v) -> c.seek(k, v));
//            return null;
//        };
//    }
}